﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Config;
using AMM.Model;
using AMM.Model.Config;

namespace AMM.Business.Config {
    public class RoleProcessor : BaseProcessor {

        private RoleRepository _roleRepository;
        public RoleProcessor() {
            this._roleRepository = new RoleRepository();
        }

        public Role FetchRoleById(int roleId) {
            try {
                Role role = this._roleRepository.FetchRoleByRoleId(roleId);
                if (role == null) {
                    role = new Role();
                }
                return role;
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定角色失败！");
            }
        }

        public List<Role> FetchRoleList(int userId = 0) {
            try {
                if (userId == 0) {
                    return this._roleRepository.FetchRoleList();
                }
                else {
                    return this._roleRepository.FetchRoleByUser(userId);
                }
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取角色列表失败！");
            }
        }

        public List<RoleViewModel> FetchRoleViewModelList(int userId = 0) {
            try {
                List<Role> resultList;
                if (userId == 0) {
                    resultList = this._roleRepository.FetchRoleList();
                }
                else {
                    resultList = this._roleRepository.FetchRoleByUser(userId);
                }

                return resultList.Select(x => {
                    return new RoleViewModel {
                        Key = x.RoleId,
                        Name = x.Name,
                        Description = x.Description,
                        IsEnabled = x.IsEnabled,
                        IsSystem = x.IsSystem,
                        CreatedBy = x.CreatedBy,
                        ModifiedBy = x.ModifiedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取角色列表失败！");
            }
        }

        public void Insert(Role role) {
            try {
                role.RoleId = Guid.NewGuid().GetHashCode();
                role.UserRoles.ToList().ForEach(x => {
                    x.RoleId = role.RoleId;
                    x.URId = Guid.NewGuid().GetHashCode();
                });
                role.RoleFunctions.ToList().ForEach(x => {
                    x.RoleId = role.RoleId;
                    x.RPId = Guid.NewGuid().GetHashCode();
                });
                role.CreatedDatetime = DateTime.Now;
                this._roleRepository.Insert(role);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新角色失败！");
            }
        }

        public void Update(Role role) {
            try {
                Role oldRole = this.FetchRoleById(role.RoleId);
                if (!oldRole.RowVersion.SequenceEqual(role.RowVersion)) {
                    throw this.BuildException("更新角色失败，数据不一致性错误！");
                }

                role.UserRoles.ToList().ForEach(x => {
                    x.URId = Guid.NewGuid().GetHashCode();
                });
                role.RoleFunctions.ToList().ForEach(x => {
                    x.RPId = Guid.NewGuid().GetHashCode();
                });
                role.ModifiedDatetime = DateTime.Now;
                this._roleRepository.Update(role);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新角色失败！");
            }
        }

        public void Delete(Role role) {
            try {
                this._roleRepository.Delete(role);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除角色失败！");
            }
        }

        public void Delete(params int[] userIds) {
            try {
                this._roleRepository.DeleteMultipleRole(userIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除角色失败！");
            }
        }
    }
}
